home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <math.h>
- #include <sys/time.h>
-
- #include <X11/Intrinsic.h>
- #include <X11/StringDefs.h>
- #include <Xm/Xm.h>
-
- #include <GL/glx.h>
- #include <GL/GLwMDrawA.h>
-
- #include "ogl.h"
- #include "vroom.h"
- #include "body.h"
- #include "car.h"
- #include "track.h"
- #include "straight.h"
- #include "solo.h"
- #include "framerate.h"
- #include "playmode.h"
- #include "sound.h"
- #include "quickcull.h"
-
- #define DEG_2_RAD ((float)0.017453293)
-
- typedef struct {
- short ascent ;
- short descent ;
- XFontStruct *xfont ;
- int base ;
- } VroomFont ;
-
-
- /* BEGIN PROTOTYPES -S ogl.c */
- static void beginOverlay( void ) ;
- static void changeLod( Widget w, XEvent *event, String *params,
- Cardinal *numParams ) ;
- static void changeSpeed( Widget w, XEvent *event, String *params,
- Cardinal *numParams ) ;
- static void changeView( Widget w, XEvent *event, String *params,
- Cardinal *numParams ) ;
- static void changeVolume( int direction ) ;
- static void commonOglInit( void ) ;
- static VroomFont * createFont( Widget w, char *resource,
- int defaultHeight ) ;
- static void createStars( void ) ;
- static void drawIcons( char *position ) ;
- static void drawIntroCB( Widget w, GLXContext *glc,
- GLwDrawingAreaCallbackStruct *cbs ) ;
- static void drawPreviewCB( Widget w, XtPointer clientData,
- GLwDrawingAreaCallbackStruct *cbs ) ;
- static void endOverlay( void ) ;
- static void endSolo( Widget w, XEvent *event, String *params,
- Cardinal *numParams ) ;
- static XFontStruct * findFont( Widget w, char *resource,
- int defaultHeight ) ;
- static int getStrWidth( VroomFont *vf, char *str, int len ) ;
- static void giveUp( Widget w, XEvent *event, String *params,
- Cardinal *numParams ) ;
- static void initIntroCB( Widget w, XtPointer clientData,
- GLwDrawingAreaCallbackStruct *cbs ) ;
- static void initMainCB( Widget w, XtPointer clientData,
- GLwDrawingAreaCallbackStruct *cbs ) ;
- static void initPreviewCB( Widget w, XtPointer clientData,
- GLwDrawingAreaCallbackStruct *cbs ) ;
- static void introDraw( void ) ;
- static void laneChangeLeft( Widget w, XEvent *event,
- String *params, Cardinal *numParams ) ;
- static void laneChangeRight( Widget w, XEvent *event,
- String *params, Cardinal *numParams ) ;
- static void quit( Widget w, XEvent *event, String *params,
- Cardinal *numParams ) ;
- static void resetView( void ) ;
- static void resizeIntroCB( Widget w, GLXContext *glc,
- GLwDrawingAreaCallbackStruct *cbs ) ;
- static void resizeMainCB( Widget w, XtPointer clientData,
- GLwDrawingAreaCallbackStruct *cbs ) ;
- static void resizePreviewCB( Widget w, XtPointer clientData,
- GLwDrawingAreaCallbackStruct *cbs ) ;
- static void setUpCull( void ) ;
- static void showArrow( void ) ;
- static void showFrameRate( void ) ;
- static void showHelp( Widget w, XEvent *event, String *params,
- Cardinal *numParams ) ;
- static void showPlayers( Widget w, XEvent *event, String *params,
- Cardinal *numParams ) ;
- static void showRestartInfo( void ) ;
- static void timeDraw( Widget w, XEvent *event, String *params,
- Cardinal *numParams ) ;
- static void toggleBars( Widget w, XEvent *event, String *params,
- Cardinal *numParams ) ;
- static void toggleDebug( Widget w, XEvent *event, String *params,
- Cardinal *numParams ) ;
- static void toggleTime( Widget w, XEvent *event, String *params,
- Cardinal *numParams ) ;
- static void volumeDown( Widget w, XEvent *event, String *params,
- Cardinal *numParams ) ;
- static void volumeUp( Widget w, XEvent *event, String *params,
- Cardinal *numParams ) ;
- /* END PROTOTYPES -S ogl.c */
-
- int baseLod = 0 ;
- int debugOn = 0 ;
- int updateStatWindow ;
- Track *introTrack[2] ;
- Widget mainOgl ;
- Widget previewOgl ;
-
- extern char *basename ;
- extern int playMode ;
- extern int freeTime ;
- extern int steering ;
- extern int self ;
- extern int nCars ;
- extern int isRobot[] ;
- extern int showSpeedBars ;
- extern long myHostId ;
- extern long gameServerId ;
- extern float currentTime ;
- extern float frameRate ;
- extern GLuint trackOutline ;
- extern GLfloat carColors[][3] ;
- extern Car cars[] ;
- extern PlayerStruct player[] ;
- extern XtAppContext appContext ;
- extern Sfx alertSfx ;
- extern Sfx changeLaneSfx ;
-
- static char *qualifyMsg = "Qualifying run." ;
- static int nRenders = 0 ;
- static int rearView ;
- static int mouseX = 0 ;
- static int mouseY = 0 ;
- static int nDrawings[MAX_LOD] ;
- static float drawingTime[MAX_LOD] ;
- static float fontDy ;
- static float fontY ;
- static float qualifyMsgWidth ;
- static GLfloat lightLoc[] = { 3.2, 0.2, 10.0, 0.0 } ;
- static GLfloat lead ;
- static GLfloat cosAzim ;
- static GLfloat cosIncl ;
- static GLfloat sinIncl ;
- static GLuint lapRecord ;
- static GLuint raceRecord ;
- static GLuint qmBox ;
- static GLuint instructions ;
- static GLuint mainSet ;
- static GLuint mapSet ;
- static GLuint mapCar ;
- static GLuint arrow ;
- static GLuint speedBars ;
- static GLuint icon[4] ;
- static GLuint stars ;
- static GLfloat raceViewW = 0.0 ;
- static GLfloat raceViewH = 0.0 ;
- static GLdouble raceViewWd = 0.0 ;
- static GLdouble raceViewHd = 0.0 ;
- static GLXContext mainGlc = NULL ;
- static GLXContext introGlc = NULL ;
- static GLXContext previewGlc = NULL ;
- static Widget introOgl ;
- static VroomFont *olf ;
- static GLubyte mapCarRaster[] = { 0xf8, 0xf8, 0xf8, 0xf8, 0xf8 } ;
-
- static GLubyte skullRaster[4*32] = {
- 0x00,0x00,0x00,0x00, 0x1c,0x00,0x00,0x38,
- 0x1c,0x00,0x00,0x38, 0x3c,0x00,0x00,0x3c,
- 0x3f,0x00,0x00,0xfc, 0x3b,0xc0,0x03,0xdc,
- 0x00,0xf0,0x0f,0x00, 0x00,0x3c,0x3c,0x00,
- 0x00,0x0f,0xf0,0x00, 0x00,0x03,0xc0,0x00,
- 0x00,0x0f,0xf0,0x00, 0x00,0x3c,0x3c,0x00,
- 0x00,0xf0,0x0f,0x00, 0x3b,0xc0,0x03,0xdc,
- 0x3f,0x00,0x00,0xfc, 0x3c,0x07,0xe0,0x3c,
- 0x1c,0x0f,0xf0,0x38, 0x1c,0x0f,0xf0,0x38,
- 0x00,0x0f,0xf0,0x00, 0x00,0x1e,0x78,0x00,
- 0x00,0x3e,0x7c,0x00, 0x00,0x3f,0xfc,0x00,
- 0x00,0x7f,0xfe,0x00, 0x00,0xfb,0xdf,0x00,
- 0x00,0xf1,0x8f,0x00, 0x00,0xf1,0x8f,0x00,
- 0x00,0xfb,0xdf,0x00, 0x00,0xff,0xff,0x00,
- 0x00,0xff,0xff,0x00, 0x00,0x7f,0xfe,0x00,
- 0x00,0x3f,0xfc,0x00, 0x00,0x0f,0xf0,0x00,
- } ;
- static GLubyte finishRaster[4*32] = {
- 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00, 0x03,0x00,0x00,0xc0,
- 0x01,0x80,0x01,0x80, 0x00,0xc0,0x03,0x00,
- 0x00,0x60,0x06,0x00, 0x00,0x30,0x0c,0x00,
- 0x02,0x18,0x18,0x40, 0x07,0x0c,0x30,0xe0,
- 0x0f,0x86,0x61,0xf0, 0x1f,0xc3,0xc3,0xf8,
- 0x3f,0xe1,0x87,0xfc, 0x7f,0xf3,0xcf,0xfe,
- 0xff,0xfe,0x7f,0xff, 0x7f,0xfc,0x3f,0xfe,
- 0x3f,0xf8,0x1f,0xfc, 0x1f,0xf0,0x0f,0xf8,
- 0x0f,0xe0,0x07,0xf0, 0x07,0xc0,0x03,0xe0,
- 0x03,0x80,0x01,0xc0, 0x01,0x00,0x00,0x80,
- 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
- } ;
- static GLubyte okRaster[4*32] = {
- 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
- 0x00,0x03,0xe0,0x00, 0x00,0x1f,0xfc,0x00,
- 0x00,0x3c,0x1e,0x00, 0x00,0xe0,0x83,0x80,
- 0x00,0xc8,0x89,0x80, 0x01,0x84,0x90,0xc0,
- 0x03,0x00,0x00,0x60, 0x03,0x40,0x01,0x60,
- 0x03,0x20,0x02,0x60, 0x06,0x00,0x00,0x30,
- 0x06,0x01,0xc0,0x30, 0x06,0xe3,0xc3,0xb0,
- 0x06,0x03,0xc0,0x30, 0x06,0x07,0x80,0x30,
- 0x03,0x2e,0x02,0x60, 0x03,0x5c,0x01,0x60,
- 0x03,0x18,0x00,0x60, 0x01,0x84,0x90,0xc0,
- 0x00,0xc8,0x89,0x80, 0x00,0xe0,0x83,0x80,
- 0x00,0x3c,0x1e,0x00, 0x00,0x1f,0xfc,0x00,
- 0x00,0x07,0xf0,0x00, 0x00,0x01,0xc0,0x00,
- 0x00,0x01,0xc0,0x00, 0x00,0x07,0xf0,0x00,
- 0x00,0x07,0xf0,0x00, 0x00,0x03,0xe0,0x00,
- 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
- } ;
-
- static char *instructionMsgs[] = {
- "Left mouse button - change 1 lane left.",
- "Right mouse button - change 1 lane right.",
- "Mouse up and down - adjust speed.",
- "Hit 'h' for detailed help.",
- } ;
-
- static GLfloat white[3] = { 1.0f, 1.0f, 1.0f } ;
- static GLfloat black[3] = { 0.0f, 0.0f, 0.0f } ;
-
- static XtActionsRec actionsTable[] = {
- { "quit", quit },
- { "laneChangeLeft", laneChangeLeft },
- { "laneChangeRight", laneChangeRight },
- { "changeSpeed", changeSpeed },
- { "changeView", changeView },
- { "changeLod", changeLod },
- { "giveUp", giveUp },
- { "showHelp", showHelp },
- { "showPlayers", showPlayers },
- { "toggleBars", toggleBars },
- { "volumeUp", volumeUp },
- { "volumeDown", volumeDown },
- #ifdef DEBUG
- { "endSolo", endSolo },
- { "toggleDebug", toggleDebug },
- { "toggleTime", toggleTime },
- { "timeDraw", timeDraw },
- #endif /* DEBUG */
- } ;
-
- static char oglTranslations[] =
- "<KeyDown>osfCancel: quit() \n"
- "<Btn3Down>: laneChangeRight() \n"
- "<Btn1Down>: laneChangeLeft() \n"
- "<Motion>: changeSpeed() \n"
- "Alt<KeyDown>d: changeLod() \n"
- "Alt<KeyDown>g: giveUp() \n"
- "<KeyDown>h: showHelp() \n"
- "<KeyDown>i: showPlayers() \n"
- "<KeyDown>v: changeView() \n"
- "<KeyDown>b: toggleBars() \n"
- "<KeyDown>osfUp: volumeUp() \n"
- "<KeyDown>osfDown: volumeDown() \n"
- #ifdef DEBUG
- "Alt<KeyDown>x: endSolo() \n"
- "<KeyDown>d: toggleDebug() \n"
- "<KeyDown>t: timeDraw() \n"
- "<KeyDown>f: toggleTime() \n"
- #endif /* DEBUG */
- ;
-
- static GLfloat poly1[4][3] = {
- { -0.5f, -0.5f, 0.0f },
- { 0.5f, -0.5f, 0.0f },
- { 0.5f, 0.5f, 0.0f },
- { -0.5f, 0.5f, 0.0f },
- } ;
- static GLfloat norm1[3] = { 0.0f, 0.0f, 1.0f } ;
- static GLfloat color1[4][3] = {
- { 1.0f, 0.0f, 0.0f },
- { 1.0f, 1.0f, 0.0f },
- { 0.0f, 1.0f, 0.0f },
- { 0.0f, 1.0f, 1.0f },
- } ;
-
-
- /*------------------------------------------------------------------------------
- * Translation callback to exit program.
- *----------------------------------------------------------------------------*/
- static void
- quit(
- Widget w,
- XEvent *event,
- String *params,
- Cardinal *numParams
- )
- {
- exitCB( w, NULL, NULL ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Translation callback when mouse moves to change speed.
- *----------------------------------------------------------------------------*/
- static void
- changeSpeed(
- Widget w,
- XEvent *event,
- String *params,
- Cardinal *numParams
- )
- {
- int my ;
-
- my = event->xmotion.y ;
-
- cars[self].desiredSpeed = ( 1.0f - (float)my / raceViewH ) * TOP_SPEED ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Draw routine.
- *----------------------------------------------------------------------------*/
- void
- drawIt(
- int startCar
- )
- {
- int i ;
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
-
-
- /*
- * Draw main window.
- */
- glCallList( mainSet ) ;
-
- glRotatef( -player[self].headingDeg, 0.0f, 0.0f, 1.0f ) ;
- glCallList( stars + baseLod ) ;
- glLightfv( GL_LIGHT0, GL_POSITION, lightLoc ) ;
- glTranslatef( -player[self].x, -player[self].y, 0.0f ) ;
-
- setUpCull() ;
-
- drawTrack() ;
-
- glEnable( GL_DEPTH_TEST ) ;
- glShadeModel( GL_SMOOTH ) ;
-
- cullDrawAllCars( startCar, nCars ) ;
-
- glShadeModel( GL_FLAT ) ;
- glDisable( GL_DEPTH_TEST ) ;
-
- /*
- * Draw map window.
- */
- glCallList( mapSet ) ;
- for( i = startCar ; i < startCar + nCars ; i++ )
- {
- glColor3fv( player[i].color ) ;
- glRasterPos2f( player[i].x, player[i].y ) ;
- glCallList( mapCar ) ;
- }
-
- /*
- * Restore push and mode change done in mapSet display list.
- */
- glPopMatrix() ;
- glMatrixMode( GL_MODELVIEW ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Callback for initialization - create GLXContext.
- *----------------------------------------------------------------------------*/
- static void
- initMainCB(
- Widget w,
- XtPointer clientData,
- GLwDrawingAreaCallbackStruct *cbs
- )
- {
- int n ;
- int maxW ;
- int wid ;
- Arg args[1] ;
- XVisualInfo *vi ;
-
- /*
- * Get GLXContext.
- */
- n = 0 ;
- XtSetArg( args[n], GLwNvisualInfo, &vi ) ; n++ ;
- XtGetValues( w, args, n ) ;
-
- mainGlc = glXCreateContext( XtDisplay( w ), vi, introGlc, GL_TRUE ) ;
- GLwDrawingAreaMakeCurrent( w, mainGlc ) ;
-
- glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ) ;
- glClearDepth( 1.0f ) ;
- glClear( GL_COLOR_BUFFER_BIT ) ;
- GLwDrawingAreaSwapBuffers( w ) ;
-
- glEnable( GL_DEPTH_TEST ) ;
-
- commonOglInit() ;
-
- olf = createFont( w, "*overlayFont", 18 ) ;
- qualifyMsgWidth = (float)getStrWidth( olf, qualifyMsg,
- strlen( qualifyMsg ) ) ;
- fontDy = (float)olf->ascent + (float)olf->descent + 1.0f ;
- glListBase( olf->base ) ;
-
- lapRecord = glGenLists( 1 ) ;
- raceRecord = glGenLists( 1 ) ;
-
- qmBox = glGenLists( 1 ) ;
- glNewList( qmBox, GL_COMPILE ) ;
- glColor3fv( black ) ;
- glBegin( GL_TRIANGLE_STRIP ) ;
- glVertex2f( -10.0f, olf->ascent + 10.0f ) ;
- glVertex2f( -10.0f, -olf->descent - 10.0f ) ;
- glVertex2f( 10.0f + qualifyMsgWidth, olf->ascent + 10.0f ) ;
- glVertex2f( 10.0f + qualifyMsgWidth, -olf->descent - 10.0f ) ;
- glEnd() ;
- glColor3fv( white ) ;
- glLineWidth( 3.0f ) ;
- glBegin( GL_LINE_LOOP ) ;
- glVertex2f( -10.0f, olf->ascent + 10.0f ) ;
- glVertex2f( -10.0f, -olf->descent - 10.0f ) ;
- glVertex2f( 10.0f + qualifyMsgWidth, -olf->descent - 10.0f ) ;
- glVertex2f( 10.0f + qualifyMsgWidth, olf->ascent + 10.0f ) ;
- glEnd() ;
- glLineWidth( 1.0f ) ;
- glEndList() ;
-
- instructions = glGenLists( 1 ) ;
- glNewList( instructions, GL_COMPILE ) ;
- maxW = 0 ;
- for( n = 0 ; n < XtNumber( instructionMsgs ) ; n++ )
- {
- wid = getStrWidth( olf, instructionMsgs[n],
- strlen( instructionMsgs[n] ) ) ;
- if( wid > maxW )
- {
- maxW = wid ;
- }
- }
- glEnable( GL_BLEND ) ;
- glColor4f( 0.0f, 0.0f, 0.0f, 0.5f ) ;
- glBegin( GL_TRIANGLE_STRIP ) ;
- glVertex2f( -5.0f - 0.5f * maxW, n * fontDy + 10.0f ) ;
- glVertex2f( -5.0f - 0.5f * maxW, 5.0f ) ;
- glVertex2f( 5.0f + 0.5f * maxW, n * fontDy + 10.0f ) ;
- glVertex2f( 5.0f + 0.5f * maxW, 5.0f ) ;
- glEnd() ;
- glDisable( GL_BLEND ) ;
- glColor3fv( white ) ;
- glBegin( GL_LINE_LOOP ) ;
- glVertex2f( -5.0f - 0.5f * maxW, n * fontDy + 10.0f ) ;
- glVertex2f( -5.0f - 0.5f * maxW, 5.0f ) ;
- glVertex2f( 5.0f + 0.5f * maxW, 5.0f ) ;
- glVertex2f( 5.0f + 0.5f * maxW, n * fontDy + 10.0f ) ;
- glEnd() ;
- glColor3f( 1.0f, 0.5f, 0.0f ) ;
- fontY = 10.0f + ( n - 1 ) * fontDy + olf->descent ;
- for( n = 0 ; n < XtNumber( instructionMsgs ) ; n++ )
- {
- wid = strlen( instructionMsgs[n] ) ;
- glRasterPos2f( -0.5f *
- getStrWidth( olf, instructionMsgs[n], wid ),
- fontY );
- glCallLists( wid, GL_UNSIGNED_BYTE,
- instructionMsgs[n] ) ;
- fontY -= fontDy ;
- }
- glEndList() ;
-
- speedBars = glGenLists( 1 ) ;
-
- /*
- * Set viewport and initialize viewing matrix.
- */
- resizeMainCB( w, 0, cbs ) ;
-
- /*
- * Create bitmap for cars in map window.
- */
- glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ) ;
- mapCar = glGenLists( 1 ) ;
- glNewList( mapCar, GL_COMPILE ) ;
- glBitmap( 5, 5, 1.0f, 1.0f, 0.0f, 0.0f, mapCarRaster ) ;
- glEndList() ;
-
- icon[0] = glGenLists( 3 ) ;
- icon[1] = icon[0] + 1 ;
- icon[2] = icon[0] + 2 ;
- icon[3] = icon[2] ;
- glNewList( icon[0], GL_COMPILE ) ;
- glBitmap( 32, 32, 32.0f, 0.0f, 0.0f, 32.0f, okRaster ) ;
- glEndList() ;
- glNewList( icon[1], GL_COMPILE ) ;
- glBitmap( 32, 32, 32.0f, 0.0f, 0.0f, 32.0f, skullRaster ) ;
- glEndList() ;
- glNewList( icon[2], GL_COMPILE ) ;
- glBitmap( 32, 32, 32.0f, 0.0f, 0.0f, 32.0f, finishRaster ) ;
- glEndList() ;
-
- /*
- * Create arrow object.
- */
- arrow = glGenLists( 1 ) ;
- glNewList( arrow, GL_COMPILE ) ;
- glColor3fv( white ) ;
- glBegin( GL_TRIANGLES ) ;
- glVertex3f( 0.0f, 0.0f, 60.0f ) ;
- glVertex3f( 30.0f, 0.0f, 90.0f ) ;
- glVertex3f( -30.0f, 0.0f, 90.0f ) ;
- glEnd() ;
- glBegin( GL_TRIANGLE_STRIP ) ;
- glVertex3f( -10.0f, 0.0f, 89.0f ) ;
- glVertex3f( 10.0f, 0.0f, 89.0f ) ;
- glVertex3f( -10.0f, 0.0f, 150.0f ) ;
- glVertex3f( 10.0f, 0.0f, 150.0f ) ;
- glEnd() ;
- glColor3fv( black ) ;
- glLineWidth( 3.0f ) ;
- glBegin( GL_LINE_LOOP ) ;
- glVertex3f( 0.0f, 0.0f, 60.0f ) ;
- glVertex3f( 30.0f, 0.0f, 90.0f ) ;
- glVertex3f( 10.0f, 0.0f, 90.0f ) ;
- glVertex3f( 10.0f, 0.0f, 150.0f ) ;
- glVertex3f( -10.0f, 0.0f, 150.0f ) ;
- glVertex3f( -10.0f, 0.0f, 90.0f ) ;
- glVertex3f( -30.0f, 0.0f, 90.0f ) ;
- glEnd() ;
- glLineWidth( 1.0f ) ;
- glEndList() ;
-
- glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
- glDisable( GL_BLEND ) ;
-
- /*
- * Initialize the viewing angle.
- */
- resetView() ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Callback when resize occurs.
- *----------------------------------------------------------------------------*/
- static void
- resizeMainCB(
- Widget w,
- XtPointer clientData,
- GLwDrawingAreaCallbackStruct *cbs
- )
- {
- int n ;
- GLfloat x ;
- GLfloat y ;
- GLfloat bnds[8] ;
- GLfloat d ;
- Window rootWin ;
- Window childWin ;
- int rootX ;
- int rootY ;
- int winX ;
- int winY ;
- unsigned int mask ;
- static int init = 0 ;
-
- GLwDrawingAreaMakeCurrent( w, mainGlc ) ;
-
- if( init == 0 )
- {
- init = 1 ;
- mainSet = glGenLists( 1 ) ;
- mapSet = glGenLists( 1 ) ;
- }
-
- raceViewW = (float)cbs->width ;
- raceViewH = (float)cbs->height ;
- raceViewWd = (double)raceViewW ;
- raceViewHd = (double)raceViewH ;
-
- glViewport( 0, 0, cbs->width, cbs->height ) ;
-
- /*
- * Set up main view matrix.
- */
- glMatrixMode( GL_PROJECTION ) ;
- glLoadIdentity() ;
- x = raceViewW / raceViewH ;
- y = 1.0f ;
- glFrustum( -x, x, -y, y, 2.0f, 4000.0f ) ;
- glMatrixMode( GL_MODELVIEW ) ;
- qcInit( x, y, 2.0f, 4000.0f ) ;
-
- /*
- * Set up matrix for map overview.
- */
- glNewList( mapSet, GL_COMPILE ) ;
- glLoadIdentity() ;
- glMatrixMode( GL_PROJECTION ) ;
- glPushMatrix() ;
- glLoadIdentity() ;
-
- getTrackBounds( bnds ) ;
- bnds[6] += 4.0f * LANE_WIDTH ;
- bnds[7] += 4.0f * LANE_WIDTH ;
- bnds[1] = bnds[4] + bnds[6] ;
- bnds[3] = bnds[5] + bnds[7] ;
- d = MAXFUNC( bnds[6], bnds[7] ) ;
- bnds[0] = bnds[4] - ( x * 5.0f - 1.0f ) * d ;
- bnds[2] = bnds[5] - ( y * 5.0f - 1.0f ) * d ;
- glOrtho( bnds[0], bnds[1], bnds[2], bnds[3], -1.0, 1.0 ) ;
- glCallList( trackOutline ) ;
- glEndList() ;
-
- glLoadIdentity() ;
-
- /*
- * Locate where the cursor is.
- */
- if( XQueryPointer( XtDisplay( w ), XtWindow( w ), &rootWin, &childWin,
- &rootX, &rootY, &winX, &winY, &mask ) )
- {
- y = (float)winY / raceViewH ;
- y = MINFUNC( 1.0f, y ) ;
- y = MAXFUNC( 0.0f, y ) ;
- cars[self].desiredSpeed = ( 1.0f - y ) * TOP_SPEED ;
- }
-
- /*
- * Create object for drawing speed bars.
- */
- x = 0.5f * raceViewW ;
- glNewList( speedBars, GL_COMPILE ) ;
- glBegin( GL_LINES ) ;
- glVertex2f( x - 9.0f, 0.9f * raceViewH ) ;
- glVertex2f( x + 9.0f, 0.9f * raceViewH ) ;
- glVertex2f( x - 9.0f, 0.8f * raceViewH ) ;
- glVertex2f( x + 9.0f, 0.8f * raceViewH ) ;
- glVertex2f( x - 9.0f, 0.7f * raceViewH ) ;
- glVertex2f( x + 9.0f, 0.7f * raceViewH ) ;
- glVertex2f( x - 9.0f, 0.6f * raceViewH ) ;
- glVertex2f( x + 9.0f, 0.6f * raceViewH ) ;
- glVertex2f( x - 9.0f, 0.5f * raceViewH ) ;
- glVertex2f( x + 9.0f, 0.5f * raceViewH ) ;
- glEnd() ;
- glEndList() ;
-
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Callback when resize occurs.
- *----------------------------------------------------------------------------*/
- static void
- resizeIntroCB(
- Widget w,
- GLXContext *glc,
- GLwDrawingAreaCallbackStruct *cbs
- )
- {
- int n ;
- GLfloat x ;
- GLfloat y ;
-
- GLwDrawingAreaMakeCurrent( w, *glc ) ;
-
- glMatrixMode( GL_PROJECTION ) ;
- glLoadIdentity() ;
- x = (float)cbs->width / (float)cbs->height ;
- y = 1.0f ;
- glFrustum( -x, x, -y, y, 2.0f, 4000.0f ) ;
- glMatrixMode( GL_MODELVIEW ) ;
- glViewport( 0, 0, cbs->width, cbs->height ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Create the main OpenGL widget.
- *----------------------------------------------------------------------------*/
- Widget
- createMainOglWidget(
- Widget parent,
- Arg pargs[],
- int pn
- )
- {
- int n ;
- Arg args[15] ;
- XtTranslations transTable ;
-
- XtAppAddActions( appContext, actionsTable, XtNumber(actionsTable) ) ;
-
- transTable = XtParseTranslationTable( oglTranslations ) ;
-
- for( n = 0 ; n < pn ; n++ )
- {
- XtSetArg( args[n], pargs[n].name, pargs[n].value ) ;
- }
- XtSetArg( args[n], GLwNrgba, True ) ; n++ ;
- XtSetArg( args[n], GLwNdoublebuffer, True ) ; n++ ;
- XtSetArg( args[n], GLwNdepthSize, 16 ) ; n++ ;
- XtSetArg( args[n], GLwNredSize, 1 ) ; n++ ;
- XtSetArg( args[n], GLwNgreenSize, 1 ) ; n++ ;
- XtSetArg( args[n], GLwNblueSize, 1 ) ; n++ ;
- XtSetArg( args[n], GLwNinstallColormap, True ) ; n++ ;
- mainOgl = XtCreateManagedWidget( "mainOgl", glwMDrawingAreaWidgetClass,
- parent, args, n ) ;
- XtAddCallback( mainOgl, GLwNginitCallback, (XtCallbackProc)initMainCB,
- NULL ) ;
- XtAddCallback( mainOgl, GLwNresizeCallback,
- (XtCallbackProc)resizeMainCB, NULL ) ;
-
- n = 0 ;
- XtSetArg( args[n], XmNtranslations, transTable ) ; n++ ;
- XtSetValues( mainOgl, args, n ) ;
-
- return( mainOgl ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Reset the view.
- *----------------------------------------------------------------------------*/
- static void
- resetView(
- void
- )
- {
- float azim ;
- float incl ;
-
- if( rearView )
- {
- azim = 180.0f ;
- lead = 150.0f ;
- }
- else
- {
- azim = 0.0f ;
- lead = -150.0f ;
- }
- incl = 35.0f ;
-
- cosAzim = cosf( azim * DEG_2_RAD ) ;
-
- cosIncl = cosf( incl * DEG_2_RAD ) ;
- sinIncl = sinf( incl * DEG_2_RAD ) ;
-
- glNewList( mainSet, GL_COMPILE ) ;
- glTranslatef( 0.0f, 0.0f, -750.0f ) ;
- glRotatef( incl, 1.0f, 0.0f, 0.0f ) ;
- glRotatef( azim, 0.0f, 1.0f, 0.0f ) ;
- glRotatef( 90.0f, -1.0f, 0.0f, 0.0f ) ;
- glTranslatef( 0.0f, lead, 0.0f ) ;
- glEndList() ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Time it.
- *----------------------------------------------------------------------------*/
- static void
- timeDraw(
- Widget w,
- XEvent *event,
- String *params,
- Cardinal *numParams
- )
- {
- int i ;
- float elapsed ;
- int nRenders = 72 ;
- struct timeval beginTime ;
- struct timeval endTime ;
-
- gettimeofday( &beginTime ) ;
- glDrawBuffer( GL_FRONT ) ;
- for( i = 0 ; i < nRenders ; i++ )
- {
- drawIt( 0 ) ;
- }
- glFinish() ;
- glDrawBuffer( GL_BACK ) ;
- gettimeofday( &endTime ) ;
- if( endTime.tv_usec < beginTime.tv_usec)
- {
- endTime.tv_usec += 1000000 ;
- endTime.tv_sec -= 1 ;
- }
- elapsed = (float)( endTime.tv_usec - beginTime.tv_usec ) / 1000000.0f
- + (float)( endTime.tv_sec - beginTime.tv_sec ) ;
-
- printf( "elapsed time = %.2f seconds, %.2f frames/second\n",
- elapsed, (float)nRenders/elapsed ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Toggle speed bars on/off.
- *----------------------------------------------------------------------------*/
- static void
- toggleBars(
- Widget w,
- XEvent *event,
- String *params,
- Cardinal *numParams
- )
- {
- showSpeedBars = !showSpeedBars ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Toggle debug on/off.
- *----------------------------------------------------------------------------*/
- static void
- toggleDebug(
- Widget w,
- XEvent *event,
- String *params,
- Cardinal *numParams
- )
- {
- debugOn = !debugOn ;
- printf( "debug %s\n", debugOn ? "on" : "off" ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Toggle freeTime on/off.
- *----------------------------------------------------------------------------*/
- static void
- toggleTime(
- Widget w,
- XEvent *event,
- String *params,
- Cardinal *numParams
- )
- {
- if( freeTime )
- {
- freeTime = 0 ;
- }
- else
- {
- freeTime = 1 ;
- }
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Pop-up a help window.
- *----------------------------------------------------------------------------*/
- static void
- showHelp(
- Widget w,
- XEvent *event,
- String *params,
- Cardinal *numParams
- )
- {
- showRaceHelpCB( w, NULL, NULL ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Change between front and rear views.
- *----------------------------------------------------------------------------*/
- static void
- changeView(
- Widget w,
- XEvent *event,
- String *params,
- Cardinal *numParams
- )
- {
- if( rearView )
- {
- rearView = 0 ;
- setMessage( "Looking forward.\n" ) ;
- }
- else
- {
- rearView = 1 ;
- setMessage( "Looking backward.\n" ) ;
- }
- resetView() ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Increase the volume.
- *----------------------------------------------------------------------------*/
- static void
- volumeUp(
- Widget w,
- XEvent *event,
- String *params,
- Cardinal *numParams
- )
- {
- changeVolume( 1 ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Decrease the volume.
- *----------------------------------------------------------------------------*/
- static void
- volumeDown(
- Widget w,
- XEvent *event,
- String *params,
- Cardinal *numParams
- )
- {
- changeVolume( -1 ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Change the volume.
- *----------------------------------------------------------------------------*/
- static void
- changeVolume(
- int direction
- )
- {
- setMessage( "Volume set to %d.", sfxVolumeChange( direction ) ) ;
- sfxPlay( alertSfx ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Change lanes to the left.
- *----------------------------------------------------------------------------*/
- static void
- laneChangeLeft(
- Widget w,
- XEvent *event,
- String *params,
- Cardinal *numParams
- )
- {
- if( CAR_RACING( cars+self ) )
- {
- if( ( cars[self].status & CHANGE_LANES ) == 0 &&
- ( playMode == VROOM_SOLO || gameServerId == myHostId ) )
- {
- sfxPlay( changeLaneSfx ) ;
- }
- cars[self].status &= ~CHANGE_LANE_RIGHT ;
- cars[self].status |= CHANGE_LANE_LEFT ;
- steering = cars[self].status & CHANGE_LANES ;
- }
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Change lanes to the right.
- *----------------------------------------------------------------------------*/
- static void
- laneChangeRight(
- Widget w,
- XEvent *event,
- String *params,
- Cardinal *numParams
- )
- {
- if( CAR_RACING( cars+self ) )
- {
- if( ( cars[self].status & CHANGE_LANES ) == 0 &&
- ( playMode == VROOM_SOLO || gameServerId == myHostId ) )
- {
- sfxPlay( changeLaneSfx ) ;
- }
- cars[self].status &= ~CHANGE_LANE_LEFT ;
- cars[self].status |= CHANGE_LANE_RIGHT ;
- steering = cars[self].status & CHANGE_LANES ;
- }
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Determine the default level of detail
- *----------------------------------------------------------------------------*/
- void
- defaultLod(
- void
- )
- {
- int i ;
- float fps ;
- static int init = 0 ;
-
- if( init == 0 )
- {
- init = 1 ;
-
- #ifdef DEBUG
- for( i = 0 ; i < MAX_LOD ; i++ )
- {
- if( drawingTime[i] > 0.0f )
- {
- printf( "lod: %d fps = %.2f\n", i,
- (float)nDrawings[i] / drawingTime[i] ) ;
- }
- else
- {
- printf( "lod: %d fps = ???\n", i ) ;
- }
- }
- #endif /* DEBUG */
- for( i = 0 ; i < MAX_LOD - 1 ; i++ )
- {
- if( drawingTime[i] > 0.0f )
- {
- fps = (float)nDrawings[i] / drawingTime[i] ;
- if( fps > 15.0f )
- {
- break ;
- }
- }
- }
-
- baseLod = i ;
- }
-
- setWorkProc( VROOM_WP_TIME_GRAPHICS, 0 ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Time graphics to determine a default lod.
- *----------------------------------------------------------------------------*/
- void
- timeGraphics(
- void
- )
- {
- int i ;
- float t0 ;
- static int init = 0 ;
-
- if( init == 0 )
- {
- for( i = 0 ; i < MAX_LOD ; i++ )
- {
- nDrawings[i] = 0 ;
- drawingTime[i] = 0.0f ;
- }
- init = 1 ;
- }
-
- baseLod = ( nRenders / 10 ) % MAX_LOD ;
- glFinish() ;
- t0 = gameTime() ;
- introDraw() ;
- glFinish() ;
- sginap( 0 ) ;
- drawingTime[baseLod] += gameTime() - t0 ;
- nDrawings[baseLod] += 1 ;
- nRenders++ ;
-
- if( nRenders >= MAX_LOD * 100 )
- {
- setWorkProc( VROOM_WP_TIME_GRAPHICS, 0 ) ;
- }
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Check for a graphics error.
- *----------------------------------------------------------------------------*/
- void
- checkGlError(
- char *label
- )
- {
- GLenum err ;
-
- while( ( err = glGetError() ) != GL_NO_ERROR )
- {
- printf( "%s: err 0x%04x\n", label, (int)err ) ;
- }
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Create the intro OpenGL widget.
- *----------------------------------------------------------------------------*/
- void
- createIntroOglWidget(
- Widget parent,
- Arg pargs[],
- int pn
- )
- {
- int n ;
- Arg args[15] ;
-
- for( n = 0 ; n < pn ; n++ )
- {
- XtSetArg( args[n], pargs[n].name, pargs[n].value ) ;
- }
- XtSetArg( args[n], GLwNrgba, True ) ; n++ ;
- XtSetArg( args[n], GLwNdoublebuffer, True ) ; n++ ;
- XtSetArg( args[n], GLwNdepthSize, 16 ) ; n++ ;
- XtSetArg( args[n], GLwNredSize, 1 ) ; n++ ;
- XtSetArg( args[n], GLwNgreenSize, 1 ) ; n++ ;
- XtSetArg( args[n], GLwNblueSize, 1 ) ; n++ ;
- XtSetArg( args[n], GLwNinstallColormap, True ) ; n++ ;
- introOgl = XtCreateManagedWidget( "introOgl",
- glwMDrawingAreaWidgetClass, parent, args, n ) ;
- XtAddCallback( introOgl, GLwNexposeCallback,
- (XtCallbackProc)drawIntroCB, &introGlc ) ;
- XtAddCallback( introOgl, GLwNginitCallback, (XtCallbackProc)initIntroCB,
- NULL ) ;
- XtAddCallback( introOgl, GLwNresizeCallback,
- (XtCallbackProc)resizeIntroCB, &introGlc ) ;
-
- baseLod = 0 ;
- setWorkProc( VROOM_WP_TIME_GRAPHICS, 1 ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Callback for initialization - create GLXContext.
- *----------------------------------------------------------------------------*/
- static void
- initIntroCB(
- Widget w,
- XtPointer clientData,
- GLwDrawingAreaCallbackStruct *cbs
- )
- {
- int n ;
- Arg args[1] ;
- XVisualInfo *vi ;
-
- /*
- * Get GLXContext.
- */
- n = 0 ;
- XtSetArg( args[n], GLwNvisualInfo, &vi ) ; n++ ;
- XtGetValues( w, args, n ) ;
-
- introGlc = glXCreateContext( XtDisplay( w ), vi, mainGlc, GL_TRUE ) ;
- GLwDrawingAreaMakeCurrent( w, introGlc ) ;
-
- glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ) ;
- glClearDepth( 1.0f ) ;
-
- glEnable( GL_DEPTH_TEST ) ;
-
- /*
- * Set viewport and initialize viewing matrix.
- */
- resizeIntroCB( w, &introGlc, cbs ) ;
-
- commonOglInit() ;
-
- introTrack[0] = createStartingTrack( 2, 800.0f ) ;
- introTrack[0]->x = 0.0f ;
- introTrack[0]->y = 0.0f ;
- introTrack[0]->theta = 0.0f ;
- introTrack[0]->number = 0 ;
- introTrack[0]->added( introTrack[0] ) ;
- introTrack[1] = createStraightTrack( 2, 800.0f ) ;
- introTrack[0]->add( introTrack[0], introTrack[1] ) ;
- introTrack[1]->number = 1 ;
- introTrack[1]->added( introTrack[1] ) ;
-
- setIntroCarShot() ;
-
- glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
- glShadeModel( GL_FLAT ) ;
- glDisable( GL_DEPTH_TEST ) ;
- glDisable( GL_LIGHTING ) ;
- glDisable( GL_BLEND ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Common graphics initialization that only needs to be done once.
- *----------------------------------------------------------------------------*/
- static void
- commonOglInit(
- void
- )
- {
- static int init = 0 ;
-
- /*
- * Define light position (accept default values for colors, etc.).
- */
- glLightfv( GL_LIGHT0, GL_POSITION, lightLoc ) ;
- glEnable( GL_LIGHT0 ) ;
-
- glShadeModel( GL_FLAT ) ;
- glDisable( GL_DEPTH_TEST ) ;
- glDisable( GL_LIGHTING ) ;
-
- glCullFace( GL_BACK ) ;
- glEnable( GL_CULL_FACE ) ;
-
- /*
- * Set to color calls change material diffuse property.
- */
- glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, 1 ) ;
- glColorMaterial( GL_FRONT, GL_DIFFUSE ) ;
-
- if( init )
- {
- return ;
- }
-
- init = 1 ;
-
- defineCarMaterials() ;
-
- /*
- * Build the car.
- */
- createCar() ;
-
- initCarColors() ;
-
- /*
- * Build background.
- */
- createStars() ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Callback for redrawing the intro window.
- *----------------------------------------------------------------------------*/
- static void
- drawIntroCB(
- Widget w,
- GLXContext *glc,
- GLwDrawingAreaCallbackStruct *cbs
- )
- {
- int realLod ;
-
- GLwDrawingAreaMakeCurrent( w, *glc ) ;
-
- glShadeModel( GL_FLAT ) ;
- glDisable( GL_DEPTH_TEST ) ;
- glDisable( GL_LIGHTING ) ;
-
- realLod = baseLod ;
- baseLod = 0 ;
- introDraw() ;
- baseLod = realLod ;
-
- GLwDrawingAreaSwapBuffers( w ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Draw the title scene.
- *----------------------------------------------------------------------------*/
- static void
- introDraw(
- void
- )
- {
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
-
- glLoadIdentity() ;
-
- glTranslatef( 0.0f, 0.0f, -235.0f ) ;
-
- glRotatef( 45.0f, 1.0f, 0.0f, 0.0f ) ;
-
- glRotatef( 165.0f, 0.0f, 1.0f, 0.0f ) ;
-
- glRotatef( 90.0f, -1.0f, 0.0f, 0.0f ) ;
-
- glCallList( stars + baseLod ) ;
-
- glTranslatef( -player[0].x, -player[0].y, 0.0f ) ;
-
- glCallList( introTrack[0]->drawMarkerList + baseLod ) ;
- glCallList( introTrack[1]->drawMarkerList + baseLod ) ;
- glColor3f( 0.4f, 0.4f, 0.4f ) ;
- glCallList( introTrack[0]->drawList + baseLod ) ;
- glCallList( introTrack[1]->drawList + baseLod ) ;
-
- glEnable( GL_LIGHTING ) ;
- glEnable( GL_DEPTH_TEST ) ;
- glShadeModel( GL_SMOOTH ) ;
-
- drawAllCars( 0, MAX_PLAYERS ) ;
-
- glShadeModel( GL_FLAT ) ;
- glDisable( GL_DEPTH_TEST ) ;
- glDisable( GL_LIGHTING ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Create a font from an X string.
- *----------------------------------------------------------------------------*/
- static VroomFont *
- createFont(
- Widget w,
- char *resource,
- int defaultHeight
- )
- {
- int i ;
- XFontStruct *font ;
- GLenum err ;
- VroomFont *vf ;
-
- font = findFont( w, resource, defaultHeight ) ;
-
- vf = myMalloc( sizeof( VroomFont ) ) ;
- vf->base = glGenLists( 127 ) ;
- glXUseXFont( font->fid, 0, 127, vf->base ) ;
- if( ( err = glGetError() ) != GL_NO_ERROR )
- {
- printf( "err #%d\n", err ) ;
- }
-
- vf->ascent = font->max_bounds.ascent ;
- vf->descent = font->max_bounds.descent ;
- vf->xfont = font ;
-
- return( vf ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Locate and load a font.
- *----------------------------------------------------------------------------*/
- static XFontStruct *
- findFont(
- Widget w,
- char *resource,
- int defaultHeight
- )
- {
- int i ;
- int delta ;
- int k ;
- int ibest ;
- XrmDatabase db ;
- char *fl ;
- XrmValue value ;
- char **fontList ;
- int nFonts ;
- XFontStruct *font ;
- XFontStruct *fontInfo ;
- static char *fontPattern = "-*-helvetica-bold-r-normal--*" ;
-
- value.addr = NULL ;
- db = XtDatabase( XtDisplay( w ) ) ;
- if( !XrmGetResource( db, resource, resource, &fl, &value )
- || ( font = XLoadQueryFont( XtDisplay( w ), value.addr ) )
- == NULL )
- {
- fprintf( stderr, "%s: could not load specified font, %s\n",
- basename, value.addr ) ;
- fontList = XListFontsWithInfo( XtDisplay( w ), fontPattern, 10,
- &nFonts, &fontInfo ) ;
- if( nFonts == 0 )
- {
- fontList = XListFonts( XtDisplay( w ), "*", 1,
- &nFonts ) ;
- if( nFonts == 0 )
- {
- fatalError( "Can not load fonts!" ) ;
- }
- fprintf( stderr, "%s: defaulting to %s\n", basename,
- fontList[0] ) ;
- font = XLoadQueryFont( XtDisplay( w ), fontList[0] ) ;
- XFreeFontNames( fontList ) ;
- }
- else
- {
- delta = fontInfo[0].ascent - fontInfo[0].descent ;
- k = delta ;
- ibest = 0 ;
- for( i = 1 ; i < nFonts ; i++ )
- {
- delta = fontInfo[i].ascent -
- fontInfo[i].descent ;
- if( delta < k && k >= defaultHeight )
- {
- ibest = i ;
- k = delta ;
- }
- else if( delta < defaultHeight && delta > k )
- {
- ibest = i ;
- k = delta ;
- }
- }
- fprintf( stderr, "%s: defaulting to %s\n", basename,
- fontList[ibest] ) ;
- font = XLoadQueryFont( XtDisplay( w ),
- fontList[ibest] ) ;
- XFreeFontInfo( fontList, fontInfo, nFonts ) ;
- }
- }
-
- return( font ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Draw overlay for pre trial run.
- *----------------------------------------------------------------------------*/
- void
- drawPreTrialOverlay(
- float t,
- char position[],
- int timeTrial,
- int showRecord
- )
- {
- char *str ;
- int i ;
- int len ;
- float g ;
- float x ;
-
- showArrow() ;
-
- beginOverlay() ;
-
- str = raceTimeString( t, &len ) ;
-
- glCallLists( 11, GL_UNSIGNED_BYTE, "Countdown: " ) ;
- glCallLists( len, GL_UNSIGNED_BYTE, str ) ;
-
- if( showRecord )
- {
- i = position[0] ;
- str = longRaceTimeString( cars[i].bestLapTime, &len ) ;
- fontY -= fontDy ;
- glRasterPos2f( 5.0f, fontY ) ;
- glCallLists( 18, GL_UNSIGNED_BYTE, "New lap record of " ) ;
- glCallLists( len, GL_UNSIGNED_BYTE, str ) ;
- glCallLists( 8, GL_UNSIGNED_BYTE, " set by " ) ;
- glCallLists( strlen( cars[i].name ), GL_UNSIGNED_BYTE,
- cars[i].name ) ;
- }
-
- drawIcons( position ) ;
-
- /*
- * In projection matrix mode, can't use glPopMatrix here because
- * it may exceed the minimum projection matrix stack size dictacted
- * by OpenGL.
- */
- glTranslatef( 0.5f * raceViewW, 0.0f, 0.0f ) ;
- glCallList( instructions ) ;
- glTranslatef( -0.5f * raceViewW, 0.0f, 0.0f ) ;
-
- if( timeTrial )
- {
- g = fmodf( t, 2.0f ) - 1.0f ;
- x = 0.5f * ( raceViewW - qualifyMsgWidth ) ;
- fontY = 0.75f * raceViewH ;
- glTranslatef( x, fontY, 0.0f ) ;
- glCallList( qmBox ) ;
- glColor3f( 1.0f, ABSFUNC( g ), 0.0f ) ;
- glRasterPos2f( 0.0f, 0.0f ) ;
- glCallLists( 15, GL_UNSIGNED_BYTE, qualifyMsg ) ;
- glTranslatef( -x, -fontY, 0.0f ) ;
- }
-
- endOverlay() ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Draw overlay for trial run.
- *----------------------------------------------------------------------------*/
- void
- drawTrialOverlay(
- float t,
- char position[]
- )
- {
- char *str ;
- int len ;
- char label[15] ;
-
- beginOverlay() ;
-
- sprintf( label, "Lap: %d", cars[self].lap ) ;
- glCallLists( 6, GL_UNSIGNED_BYTE, label ) ;
-
- fontY -= fontDy ;
- glRasterPos2f( 5.0f, fontY ) ;
- sprintf( label, "Time: " ) ;
- glCallLists( 6, GL_UNSIGNED_BYTE, label ) ;
- str = raceTimeString( t, &len ) ;
- glCallLists( len, GL_UNSIGNED_BYTE, str ) ;
-
- fontY -= fontDy ;
- glRasterPos2f( 5.0f, fontY ) ;
- glCallList( lapRecord ) ;
-
- #ifdef DEBUG
- showFrameRate() ;
- #endif /* DEBUG */
-
- if( cars[self].status & CAR_WIPE_OUT )
- {
- showRestartInfo() ;
- }
-
- drawIcons( position ) ;
-
- endOverlay() ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Draw the status symbols.
- *----------------------------------------------------------------------------*/
- static void
- drawIcons(
- char *position
- )
- {
- int i ;
- int n ;
- float y ;
-
- y = MAX_PLAYERS * 38.0f + 2.0f ;
- for( i = 0 ; i < MAX_PLAYERS ; i++ )
- {
- n = position[i] ;
- y -= 38.0f ;
- glColor3fv( carColors[n] ) ;
- glRasterPos2f( raceViewW - 2.0f, y ) ;
- glCallList( icon[cars[n].status & 0x03] ) ;
- }
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Draw overlay for race.
- *----------------------------------------------------------------------------*/
- void
- drawRaceOverlay(
- float t,
- int laps,
- int dLaps,
- int ahead,
- char position[]
- )
- {
- char *str ;
- int len ;
- char label[26] ;
-
- beginOverlay() ;
-
- sprintf( label, "Laps: %2d ", laps ) ;
- glCallLists( 9, GL_UNSIGNED_BYTE, label ) ;
- fontY -= fontDy ;
- glRasterPos2f( 5.0f, fontY ) ;
- if( ahead )
- {
- sprintf( label, "Ahead %2d laps", dLaps ) ;
- len = 13 ;
- }
- else
- {
- sprintf( label, "Back %2d laps", dLaps ) ;
- len = 12 ;
- }
- if( dLaps == 1 )
- {
- len-- ;
- }
- glCallLists( len, GL_UNSIGNED_BYTE, label ) ;
-
- fontY -= fontDy ;
- glRasterPos2f( 5.0f, fontY ) ;
- glCallLists( 6, GL_UNSIGNED_BYTE, "Time: " ) ;
- str = raceTimeString( t, &len ) ;
- glCallLists( len, GL_UNSIGNED_BYTE, str ) ;
-
- fontY -= fontDy ;
- glRasterPos2f( 5.0f, fontY ) ;
- glCallList( raceRecord ) ;
-
- #ifdef DEBUG
- showFrameRate() ;
- #endif /* DEBUG */
-
- if( cars[self].status & CAR_WIPE_OUT )
- {
- showRestartInfo() ;
- }
-
- drawIcons( position ) ;
-
- endOverlay() ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Draw overlay for post trial run.
- *----------------------------------------------------------------------------*/
- void
- drawPostTrialOverlay(
- float t,
- char position[]
- )
- {
- char *str ;
- int len ;
-
- beginOverlay() ;
-
- str = longRaceTimeString( t, &len ) ;
-
- glCallLists( 10, GL_UNSIGNED_BYTE, "Best Lap: " ) ;
- glCallLists( len, GL_UNSIGNED_BYTE, str ) ;
-
- fontY -= fontDy ;
- glRasterPos2f( 5.0f, fontY ) ;
- glCallList( lapRecord ) ;
-
- drawIcons( position ) ;
-
- endOverlay() ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Draw overlay for post race.
- *----------------------------------------------------------------------------*/
- void
- drawPostRaceOverlay(
- float finishTime,
- float raceTime,
- char position[]
- )
- {
- char *str ;
- int len ;
-
- beginOverlay() ;
-
- str = raceTimeString( finishTime, &len ) ;
-
- glCallLists( 12, GL_UNSIGNED_BYTE, "Final time: " ) ;
- glCallLists( len, GL_UNSIGNED_BYTE, str ) ;
-
- fontY -= fontDy ;
- glRasterPos2f( 5.0f, fontY ) ;
- glCallList( raceRecord ) ;
-
- fontY -= fontDy ;
- str = raceTimeString( raceTime, &len ) ;
- glRasterPos2f( 5.0f, fontY ) ;
- glCallLists( 6, GL_UNSIGNED_BYTE, "Time: " ) ;
- glCallLists( len, GL_UNSIGNED_BYTE, str ) ;
-
- drawIcons( position ) ;
-
- endOverlay() ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Set up for overlay drawing.
- *----------------------------------------------------------------------------*/
- static void
- beginOverlay(
- void
- )
- {
- /*
- * Load identity matrix on the model view matrix.
- */
- glPushMatrix() ;
- glLoadIdentity() ;
-
- /*
- * Save the old projection matrix and initialize to ortho.
- */
- glMatrixMode( GL_PROJECTION ) ;
- glPushMatrix() ;
- glLoadIdentity() ;
-
- glOrtho( 0.0, raceViewWd, 0.0, raceViewHd, -1.0, 1.0 ) ;
-
- glColor3fv( white ) ;
-
- if( showSpeedBars )
- glCallList( speedBars ) ;
-
- fontY = raceViewH - olf->ascent - 2.0f ;
- glRasterPos2f( 5.0f, fontY ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * End overlay drawing.
- *----------------------------------------------------------------------------*/
- static void
- endOverlay(
- void
- )
- {
- /*
- * Restore the old projection matrix.
- */
- glPopMatrix() ;
- /*
- * Restore the model view matrix.
- */
- glMatrixMode( GL_MODELVIEW ) ;
- glPopMatrix() ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Give up the race.
- *----------------------------------------------------------------------------*/
- static void
- giveUp(
- Widget w,
- XEvent *event,
- String *params,
- Cardinal *numParams
- )
- {
- giveUpRace() ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Change the LOD setting.
- *----------------------------------------------------------------------------*/
- static void
- changeLod(
- Widget w,
- XEvent *event,
- String *params,
- Cardinal *numParams
- )
- {
- baseLod = ( baseLod + 1 ) % MAX_LOD ;
-
- setCarBaseLod( baseLod ) ;
-
- setTrackBaseLod( baseLod ) ;
-
- setMessage( "LOD set to %d.", baseLod ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Show instructions for restarting car after a wipeout.
- *----------------------------------------------------------------------------*/
- static void
- showRestartInfo(
- void
- )
- {
- static char *msg = "Place cursor below line to restart." ;
-
- glBegin( GL_LINES ) ;
- glVertex2f( 0.0f, 0.1f * raceViewH ) ;
- glVertex2f( raceViewW, 0.1f * raceViewH ) ;
- glEnd() ;
-
- glRasterPos2f( 5.0f, 0.1f * raceViewH + olf->descent + 2.0f ) ;
- glCallLists( 35, GL_UNSIGNED_BYTE, msg ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Translation callback to end solo mode (debug).
- *----------------------------------------------------------------------------*/
- static void
- endSolo(
- Widget w,
- XEvent *event,
- String *params,
- Cardinal *numParams
- )
- {
- endSoloGame() ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Create the preview OpenGL widget.
- *----------------------------------------------------------------------------*/
- void
- createPreviewOglWidget(
- Widget parent,
- Arg pargs[],
- int pn
- )
- {
- int n ;
- Arg args[15] ;
-
- for( n = 0 ; n < pn ; n++ )
- {
- XtSetArg( args[n], pargs[n].name, pargs[n].value ) ;
- }
- XtSetArg( args[n], GLwNrgba, True ) ; n++ ;
- XtSetArg( args[n], GLwNdoublebuffer, True ) ; n++ ;
- XtSetArg( args[n], GLwNdepthSize, 16 ) ; n++ ;
- XtSetArg( args[n], GLwNredSize, 1 ) ; n++ ;
- XtSetArg( args[n], GLwNgreenSize, 1 ) ; n++ ;
- XtSetArg( args[n], GLwNblueSize, 1 ) ; n++ ;
- XtSetArg( args[n], GLwNinstallColormap, True ) ; n++ ;
- previewOgl = XtCreateManagedWidget( "previewOgl",
- glwMDrawingAreaWidgetClass, parent, args, n ) ;
- XtAddCallback( previewOgl, GLwNexposeCallback,
- (XtCallbackProc)drawPreviewCB, NULL ) ;
- XtAddCallback( previewOgl, GLwNresizeCallback,
- (XtCallbackProc)resizePreviewCB, NULL ) ;
- XtAddCallback( previewOgl, GLwNginitCallback,
- (XtCallbackProc)initPreviewCB, NULL ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Callback for initialization - create GLXContext.
- *----------------------------------------------------------------------------*/
- static void
- initPreviewCB(
- Widget w,
- XtPointer clientData,
- GLwDrawingAreaCallbackStruct *cbs
- )
- {
- int n ;
- Arg args[1] ;
- XVisualInfo *vi ;
-
- /*
- * Get GLXContext.
- */
- n = 0 ;
- XtSetArg( args[n], GLwNvisualInfo, &vi ) ; n++ ;
- XtGetValues( w, args, n ) ;
-
- previewGlc = glXCreateContext( XtDisplay( w ), vi, introGlc, GL_TRUE ) ;
- GLwDrawingAreaMakeCurrent( w, previewGlc ) ;
-
- glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ) ;
- glClearDepth( 1.0f ) ;
- glClear( GL_COLOR_BUFFER_BIT ) ;
- GLwDrawingAreaSwapBuffers( w ) ;
-
- glEnable( GL_DEPTH_TEST ) ;
-
- commonOglInit() ;
-
- /*
- * Set viewport and initialize viewing matrix.
- */
- resizePreviewCB( w, 0, cbs ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Callback when resize occurs.
- *----------------------------------------------------------------------------*/
- static void
- resizePreviewCB(
- Widget w,
- XtPointer clientData,
- GLwDrawingAreaCallbackStruct *cbs
- )
- {
- if( previewGlc == NULL )
- {
- initPreviewCB( w, clientData, cbs ) ;
- }
- else if( previewGlc != NULL )
- {
- GLwDrawingAreaMakeCurrent( w, previewGlc ) ;
-
- glViewport( 0, 0, cbs->width, cbs->height ) ;
- }
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Draw preview window.
- *----------------------------------------------------------------------------*/
- static void
- drawPreviewCB(
- Widget w,
- XtPointer clientData,
- GLwDrawingAreaCallbackStruct *cbs
- )
- {
- GLfloat d ;
- GLfloat x ;
- GLfloat y ;
- GLfloat bnds[8] ;
-
- if( previewGlc == NULL )
- {
- initPreviewCB( w, clientData, cbs ) ;
- }
- GLwDrawingAreaMakeCurrent( w, previewGlc ) ;
-
- glShadeModel( GL_FLAT ) ;
- glDisable( GL_DEPTH_TEST ) ;
- glDisable( GL_LIGHTING ) ;
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
-
- if( cbs->width > cbs->height )
- {
- x = (float)cbs->width / (float)cbs->height ;
- y = 1.0f ;
- }
- else
- {
- x = 1.0f ;
- y = (float)cbs->height / (float)cbs->width ;
- }
- getTrackBounds( bnds ) ;
-
- glMatrixMode( GL_PROJECTION ) ;
- glLoadIdentity() ;
- bnds[6] += 4.0f * LANE_WIDTH ;
- bnds[7] += 4.0f * LANE_WIDTH ;
- d = MAXFUNC( bnds[6], bnds[7] ) ;
- bnds[0] = bnds[4] - x * d ;
- bnds[1] = bnds[4] + x * d ;
- bnds[2] = bnds[5] - y * d ;
- bnds[3] = bnds[5] + y * d ;
- glOrtho( bnds[0], bnds[1], bnds[2], bnds[3], -1.0, 1.0 ) ;
-
- glMatrixMode( GL_MODELVIEW ) ;
- glLoadIdentity() ;
- glCallList( trackOutline ) ;
-
- GLwDrawingAreaSwapBuffers( w ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Show an arrow above player's car.
- *----------------------------------------------------------------------------*/
- static void
- showArrow(
- void
- )
- {
- glPushMatrix() ;
- glCallList( mainSet ) ;
- if( rearView )
- {
- glScalef( -1.0f, 1.0f, 1.0f ) ;
- }
- glCallList( arrow ) ;
- glPopMatrix() ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Return the width of a string.
- *----------------------------------------------------------------------------*/
- static int
- getStrWidth(
- VroomFont *vf,
- char *str,
- int len
- )
- {
- return( XTextWidth( vf->xfont, str, len ) ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Display the list of players.
- *----------------------------------------------------------------------------*/
- static void
- showPlayers(
- Widget w,
- XEvent *event,
- String *params,
- Cardinal *numParams
- )
- {
- static char *hs = "human" ;
- static char *rs = "robot" ;
-
- if( playMode == VROOM_TEAM )
- {
- postInfo( NULL, NULL,
- "Red: %s (%s)\n\n"
- "Green: %s (%s)\n\n"
- "Blue: %s (%s)\n\n"
- "Yellow: %s (%s)\n\n"
- "Pink: %s (%s)\n\n"
- "Cyan: %s (%s)\n\n"
- "Orange: %s (%s)\n\n"
- "White: %s (%s)",
- cars[0].name, ( isRobot[0] ? rs : hs ),
- cars[1].name, ( isRobot[1] ? rs : hs ),
- cars[2].name, ( isRobot[2] ? rs : hs ),
- cars[3].name, ( isRobot[3] ? rs : hs ),
- cars[4].name, ( isRobot[4] ? rs : hs ),
- cars[5].name, ( isRobot[5] ? rs : hs ),
- cars[6].name, ( isRobot[6] ? rs : hs ),
- cars[7].name, ( isRobot[7] ? rs : hs ) ) ;
- }
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Print out the frame rate.
- *----------------------------------------------------------------------------*/
- static void
- showFrameRate(
- void
- )
- {
- char fr[6] ;
-
- fontY -= fontDy ;
- glRasterPos2f( 5.0f, fontY ) ;
- sprintf( fr, "%.1f", frameRate ) ;
- glCallLists( strlen( fr ), GL_UNSIGNED_BYTE, fr ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Set up cull view.
- *----------------------------------------------------------------------------*/
- static void
- setUpCull(
- void
- )
- {
- float ci ;
- float ca ;
- float e[3] ;
- float vl[3] ;
- float vw[3] ;
- float vh[3] ;
- float s ;
- float ang ;
-
- ci = 750.0f * cosIncl ;
- ca = cosAzim * ci ;
-
- ang = player[self].headingDeg * DEG_2_RAD ;
- vl[0] = -sinf( ang ) ;
- vl[1] = cosf( ang ) ;
- vl[2] = -750.0f * sinIncl ;
-
- e[0] = player[self].x - ( lead + ca ) * vl[0] ;
- e[1] = player[self].y - ( lead + ca ) * vl[1] ;
- e[2] = -vl[2] ;
-
- vl[0] *= ca ;
- vl[1] *= ca ;
-
- s = 1.0f / sqrtf( vl[0] * vl[0] + vl[1] * vl[1] + vl[2] * vl[2] ) ;
- vl[0] *= s ;
- vl[1] *= s ;
- vl[2] *= s ;
-
- vw[0] = vl[1] ;
- vw[1] = -vl[0] ;
- vw[2] = 0.0f ;
- s = 1.0f / sqrtf( vw[0] * vw[0] + vw[1] * vw[1] ) ;
- vw[0] *= s ;
- vw[1] *= s ;
-
- vh[0] = vw[1] * vl[2] - vw[2] * vl[1] ;
- vh[1] = vw[2] * vl[0] - vw[0] * vl[2] ;
- vh[2] = vw[0] * vl[1] - vw[1] * vl[0] ;
-
- qcSetView( e, vl, vw, vh ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Create the star background.
- *----------------------------------------------------------------------------*/
- static void
- createStars(
- void
- )
- {
- int i ;
- int j ;
- int k ;
- int n ;
- int rndMask = 0x0fff ;
- float rndDiv = 1.0f / 4095.0f ;
- float phi ;
- float phi0 ;
- float phi1 ;
- float phiFactor ;
- float theta ;
- float theta0 ;
- float theta1 ;
- float thetaFactor ;
- float dS ;
- float S ;
- const int nTheta = 11 ;
- const int nPhi = 5 ;
- const int nStars = 150 ;
-
- thetaFactor = 360.0f * DEG_2_RAD / (float)( nTheta ) ;
- phiFactor = 85.0f * DEG_2_RAD / (float)( nPhi ) ;
- stars = glGenLists( MAX_LOD ) ;
-
- /*
- * Find total surface size of star display area.
- */
- S = ( sinf( 15.0f * DEG_2_RAD ) - sinf( -70.0f * DEG_2_RAD ) ) *
- 2.0f * (float)M_PI ;
-
- for( i = MAX_LOD - 1 ; i >= 0 ; i-- )
- {
- glNewList( stars+i, GL_COMPILE ) ;
- if( i == MAX_LOD - 1 )
- {
- glColor3f( 1.0f, 1.0f, 1.0f ) ;
- }
- for( n = i + 1 ; n < MAX_LOD ; n++ )
- {
- glCallList( stars + n ) ;
- }
- glBegin( GL_POINTS ) ;
- for( j = 0 ; j < nTheta ; j++ )
- {
- theta0 = j * thetaFactor ;
- theta1 = theta0 + thetaFactor ;
- for( k = 0 ; k < nPhi ; k++ )
- {
- phi0 = -70.0f * DEG_2_RAD + k * phiFactor ;
- phi1 = phi0 + phiFactor ;
- dS = ( sinf( phi1 ) - sinf( phi0 ) ) *
- ( theta1 - theta0 ) ;
- /*
- * Calculate how many stars in this quadrant.
- * (Avoids clustering of stars near poles.)
- */
- n = (int)( (float)nStars * dS / S ) ;
- while( n-- > 0 )
- {
- theta = ( rand() & rndMask ) * rndDiv ;
- theta = theta0 +
- theta * ( theta1 - theta0 ) ;
- phi = ( rand() & rndMask ) * rndDiv ;
- phi = phi0 + phi * ( phi1 - phi0 ) ;
- glVertex3f( 1000.f * cosf(phi) *
- cosf(theta),
- 1000.0f * cosf( phi ) *
- sinf( theta ),
- 1000.0f * sinf( phi ) ) ;
- }
- }
- }
- glEnd() ;
- glEndList() ;
- }
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Create display lists for printing out the track lap and race records.
- *----------------------------------------------------------------------------*/
- void
- noteTrackRecords(
- void
- )
- {
- char *str ;
- int len ;
- static char *r = "Record: " ;
-
- str = longRaceTimeString( getTrackLapRecordTime(), &len ) ;
- glNewList( lapRecord, GL_COMPILE ) ;
- glCallLists( 8, GL_UNSIGNED_BYTE, r ) ;
- glCallLists( len, GL_UNSIGNED_BYTE, str ) ;
- glEndList() ;
-
- str = longRaceTimeString( getTrackRaceRecordTime(), &len ) ;
- glNewList( raceRecord, GL_COMPILE ) ;
- glCallLists( 8, GL_UNSIGNED_BYTE, r ) ;
- glCallLists( len, GL_UNSIGNED_BYTE, str ) ;
- glEndList() ;
- }
-